package com.ctgu.common.util;

import com.ctgu.module.admin.model.Email;
import com.ctgu.module.admin.model.Mailbox;
import org.springframework.mail.javamail.JavaMailSenderImpl;
import org.springframework.mail.javamail.MimeMessageHelper;

import javax.mail.MessagingException;

public class MailUtil {
    public static void sendEmail(Mailbox mailbox, Email email) throws MessagingException {
        JavaMailSenderImpl sender = new JavaMailSenderImpl();
        // 设置发件人信息
        sender.setHost(mailbox.getSmtpAddress());
        sender.setPort(mailbox.getSmtpPort());
        sender.setProtocol("smtps");
        sender.setUsername(mailbox.getEmailAddress());
        sender.setPassword(mailbox.getCode());
        sender.setDefaultEncoding("utf-8");

        MimeMessageHelper messageHelper = new MimeMessageHelper(sender.createMimeMessage(),true);
        messageHelper.setFrom(mailbox.getEmailAddress());
        messageHelper.setTo(email.getAddress());
        messageHelper.setSubject(email.getTitle());
        messageHelper.setText(email.getContent(), true);

        sender.send(messageHelper.getMimeMessage());
    }
}
